﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;

namespace Framework.Utils
{
    public static class Utils
    {
        /// <summary>
        /// 转化整型, 如果解析不出为0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(string str)
        {
            try
            {
                return int.Parse(str);
            }
            catch (Exception) { }
            return 0;
        }

        /// <summary>
        /// 转化数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T ToValue<T>(object value)
        {
            if (value == null) { return default(T); }
            // 空字符串处理
            Type vtype = value.GetType();
            if (vtype == typeof(string) || vtype == typeof(string))
            {
                if (string.IsNullOrEmpty((string)value))
                {
                    return default(T);
                }
            }
            // 转化对象
            Type type = typeof(T);
            try
            {
                // bool 特殊处理
                if (type == typeof(bool) || type == typeof(Boolean))
                {
                    int v = ToValue<int>(value);
                    if (v > 0) { return (T)((Object)true); }
                }
                return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
            }
            catch (Exception) { }
            return default(T);
        }

        /// <summary>
        /// 转化基础对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static object ToBaseValue(string str)
        {
            //转化成浮点型
            try
            {
                float n = float.Parse(str);
                return n;
            }
            catch (Exception) { }

            //转化整型
            try
            {
                int n = int.Parse(str);
                return n;
            }
            catch (Exception) { }

            //只能是字符串
            return str;
        }

    }

    /// <summary>
    /// 枚举工具
    /// </summary>
    public static class EnumUtils
    {

        /// <summary>
        /// 获取枚举上的属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="e"></param>
        /// <returns></returns>
        public static T GetCustomAttribute<T>(Enum e) where T : Attribute
        {
            if (e == null) { return null; }
            Type etype = e.GetType();
            string eunmName = Enum.GetName(etype, e);
            if (eunmName == null) { return null; }
            FieldInfo enumInfo = etype.GetField(eunmName);
            if (enumInfo == null) { return null; }
            return enumInfo.GetCustomAttribute<T>();
        }

        /// <summary>
        /// 获取枚举的文本注解内容
        /// </summary>
        public static string GetEnumString(Enum e)
        {
            DescriptionAttribute attributes = EnumUtils.GetCustomAttribute<DescriptionAttribute>(e);
            return (attributes != null) ? attributes.Description : e.ToString();
        }
    }


}
